﻿using System;
using System.Collections;

namespace Eneta.Portal.Forum.Core.Domain
{
    public class UniversalSorter : IComparer
    {
        private readonly SortDirection _direction;
        private readonly string _sortPropertyName;

        public UniversalSorter(string sortPropertyName)
        {
            _sortPropertyName = sortPropertyName;
            _direction = SortDirection.Ascending;
        }

        public UniversalSorter(string sortPropertyName, SortDirection direction)
        {
            _sortPropertyName = sortPropertyName;
            _direction = direction;
        }

        public int Compare(object x, object y)
        {
            var obj2 = x.GetType().GetProperty(_sortPropertyName).GetValue(x, null);
            var obj3 = y.GetType().GetProperty(_sortPropertyName).GetValue(y, null);

            if (_direction == SortDirection.Ascending)
            {
                return ((IComparable)obj2).CompareTo(obj3);
            }
            return ((IComparable)obj3).CompareTo(obj2);
        }
    }
}
